package cn.tedu.eguser1.controller;
import cn.tedu.eguser1.pojo.dto.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;  // 引入User类
import java.util.Date;  // 引入Date类
import java.util.List;

import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.dto.UserAddParam;
import cn.tedu.eguser1.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
   @Autowired
    private UserMapper UserMapper;
    @Autowired
    private UserMapper userMapper;


    /**
    * 1.添加用户
    */
   @PostMapping("add")
    public String add(UserAddParam userAddParam) {
       System.out.println("userAddParam:"+userAddParam);
       //创建实体类
       User user = new User();
       BeanUtils.copyProperties(userAddParam,user);
       //处理2个时间
       user.setCreateTime(new Date());
       user.setUpdateTime(new Date());
       //调用接口
       UserMapper.insertUser(user);
       return "添加成功";
   }
    /**2.用户列表*/
    @GetMapping("list")
    public List<UserListVO>list() {
        return userMapper.selectUserlist();
    }
    @DeleteMapping("del")
    public String del(long id) {
        System.out.println("del:"+id);
        int i = userMapper.del(id);
        if(i>0){
            return "删除用户成功";
        }return "删除失败";
    }
    /**4.更新指定用户*/
    @PostMapping("update")
    public String update(UserUpdateParam userUpdateParam) {
        System.out.println("userUpdateParam:"+userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int i = userMapper.updateUser(user);
        if(i>0){
            return "更新成功";
        }return "更新失败";
    }
}
